package com.base.web.auth.controller;

import com.base.web.auth.service.RoleService;
import com.base.web.core.common.BaseConstant;
import com.base.web.core.common.JsonResult;
import com.base.web.core.util.UuidUtil;
import com.base.web.model.master.auth.Role;
import com.base.web.model.master.auth.RoleResource;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:角色控制类
 *
 * @Author: rentie
 * @Date: 2018/7/10 14:45
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    private Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @PostMapping("/add")
    public JsonResult save(Role bean) {
        Integer count;
        bean.setId(UuidUtil.getUUID32());
        try {
            count = roleService.insertSelective(bean);
        } catch (Exception e) {
            logger.error("添加角色异常={}", e.getMessage());
            return JsonResult.fail("添加角色异常");
        }
        return JsonResult.success(count);
    }

    @PostMapping("/del")
    public JsonResult del(Role bean) {
        Integer count;
        try {
            bean.setStatus(BaseConstant.DATA_STATUS_DELETE);
            count = roleService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("删除角色异常={}", e.getMessage());
            return JsonResult.fail("删除角色异常");
        }
        return JsonResult.success(count);
    }


    @PostMapping("/update")
    public JsonResult update(Role bean) {
        Integer count;
        try {
            count = roleService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("修改角色异常={}", e.getMessage());
            return JsonResult.fail("修改角色异常");
        }
        return JsonResult.success(count);
    }

    @PostMapping("/statusChange")
    public JsonResult statusChange(Role bean) {
        Integer count;
        try {
            count = roleService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("启用或停用角色异常={}", e.getMessage());
            return JsonResult.fail("启用或停用角色异常");
        }
        return JsonResult.success(count);
    }

    /**
     * 角色管理查询角色列表
     *
     * @param map page   当前页
     *            limit 每页个数
     * @return
     */
    @GetMapping("/managelist")
    public JsonResult managelist(@RequestParam Map<String, String> map) {
        PageInfo<Role> list = null;
        try {
            list = roleService.queryManageList(map);
        } catch (Exception e) {
            logger.error("查询角色异常={}", e.getMessage());
            return JsonResult.fail("查询角色异常");
        }
        return JsonResult.success(list.getList(), list.getTotal());
    }

    /**
     * 角色绑定查询角色列表
     *
     * @param map page   当前页
     *            limit 每页个数
     * @return
     */
    @GetMapping("/list")
    public JsonResult list(@RequestParam Map<String, String> map) {
        PageInfo<Role> list = null;
        try {
            list = roleService.select(Role.defaultCondition("1"),map);
        } catch (Exception e) {
            logger.error("查询角色异常={}", e.getMessage());
            return JsonResult.fail("查询角色异常");
        }
        return JsonResult.success(list.getList(), list.getTotal());
    }

    @PostMapping("/addCheckRoleName")
    public JsonResult addCheckRoleName(Role bean) {
        Role temp;
        try {
            temp = roleService.selectOne(bean);
            if (temp != null) {
                return JsonResult.success();
            }
        } catch (Exception e) {
            logger.error("检查角色名异常={}", e.getMessage());
            return JsonResult.fail("检查角色名异常");
        }
        return JsonResult.fail();
    }

    @PostMapping("/editCheckRoleName")
    public JsonResult editCheckRoleName(Role bean) {
        Role temp;
        try {
            String id = bean.getId();
            bean.setId(null);
            temp = roleService.selectOne(bean);
            if (temp != null && !Objects.equals(temp.getId(), id)) {
                return JsonResult.success();
            }
        } catch (Exception e) {
            logger.error("检查角色名异常={}", e.getMessage());
            return JsonResult.fail("检查角色名异常");
        }
        return JsonResult.fail();
    }

    /**
     * 角色授权
     * @param userId 用户id 用于清空缓存
     * @param roleId 角色id
     * @param menuIds 资源id数组
     * @return
     */
    @PostMapping("/authorization")
    public JsonResult authorization(@RequestParam("userId") String userId,@RequestParam("roleId") String roleId,@RequestParam("menuIds")String[] menuIds) {
        List<RoleResource> list = new ArrayList<RoleResource>();
        for (String s:menuIds) {
            list.add(new RoleResource(roleId,s));
        }
        try {
            roleService.roleAuthorization(userId,roleId,list);
        } catch (Exception e) {
            logger.error("角色授权异常={}", e.getMessage());
            return JsonResult.fail("角色授权异常");
        }
        return JsonResult.success(1);
    }


}